package cn.com.platform.service;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.com.easy.exception.BusinessException;
import cn.com.easy.permission.model.UserModel;
import cn.com.easy.utils.Page;
import cn.com.easy.utils.PageRequest;
import cn.com.easy.utils.PageRequestUtils;
import cn.com.easy.utils.PageUtils;
import cn.com.platform.dao.AccessoryDao;
import cn.com.platform.dao.ArticleCateDao;
import cn.com.platform.dao.ArticleCommentDao;
import cn.com.platform.dao.ArticleContentDao;
import cn.com.platform.dao.ArticleDao;
import cn.com.platform.entity.AccessoryEntity;
import cn.com.platform.entity.ArticleCateEntity;
import cn.com.platform.entity.ArticleCommentEntity;
import cn.com.platform.entity.ArticleContentEntity;
import cn.com.platform.entity.ArticleEntity;
import cn.com.platform.web.manage.dto.ArticleDto;

/***
 * 文章服务类
 * 
 * @author chenbinghuang 2017年6月14日
 * 
 */
@Service
public class ArticleService {
	@Autowired
	private ArticleCateDao articleCateDao;
	@Autowired
	private ArticleDao articleDao;
	@Autowired
	private ArticleContentDao articleContentDao;
	@Autowired
	private ArticleCommentDao articleCommentDao;
	/** 文件服务类 */
	@Autowired
	private AccessoryDao accessoryDao;

	/***
	 * 根据id获取文章
	 * 
	 * @param id
	 * @return
	 * @author chenbinghuang 2017年6月14日
	 */
	public ArticleEntity getById(Long id) {
		if (id != null) {
			return articleDao.findOne(id);
		}
		return null;
	}

	/***
	 * 通过上级分类id获取文章分类列表
	 * 
	 * @param parentId
	 * @return
	 * @auth nibili 2018年7月17日
	 */
	public List<ArticleCateEntity> getCateListByParentId(Long parentId) {
		return articleCateDao.getByParentIdAndHidden(parentId, false);
	}

	/***
	 * 最新资讯
	 * 
	 * @param top
	 * @return
	 * @author chenbinghuang 2017年6月14日
	 */
	@Cacheable(key = "'getHotArticleList'+#top", value = "dayFastjson")
	public List<ArticleEntity> getNewArticleList(int top) {
		PageRequest pageRequest = new PageRequest(1, top);
		pageRequest.setOrderBy("dateline,id");
		pageRequest.setOrderDir("desc,desc");
		List<ArticleEntity> list = articleDao.getArticleList(PageRequestUtils.buildSpringDataPageRequest(pageRequest));
		return list;
	}

	/***
	 * 随机资讯
	 * 
	 * @param top
	 * @return
	 * @author chenbinghuang 2017年6月14日
	 */
	@Cacheable(key = "'getRandArticleList'+#top", value = "dayFastjson")
	public List<ArticleEntity> getRandArticleList(int top) {
		PageRequest pageRequest = new PageRequest(1, top);
		pageRequest.setOrderBy("rand()");
		pageRequest.setOrderDir("asc");
		List<ArticleEntity> list = articleDao.getArticleList(PageRequestUtils.buildSpringDataPageRequest(pageRequest));
		return list;
	}

	/***
	 * 热门资讯（点击数）
	 * 
	 * @param cityId
	 * @param top
	 * @return
	 * @author chenbinghuang 2017年6月14日
	 */
	@Cacheable(key = "'getHotArticleList'+#cityId+'-'+#top", value = "dayFastjson")
	public List<ArticleEntity> getHotArticleList(Long cityId, int top) {
		PageRequest pageRequest = new PageRequest(1, top);
		pageRequest.setOrderBy("views,dateline");
		pageRequest.setOrderDir("desc,desc");
		List<ArticleEntity> list = articleDao.getArticleList(PageRequestUtils.buildSpringDataPageRequest(pageRequest));
		return list;
	}

	/***
	 * 根据单个分类id获取文章
	 * 
	 * @param cateId
	 * @param pageRequest
	 * @return
	 * @author chenbinghuang 2017年6月14日
	 */
	public Page<ArticleEntity> getArticleListByCateId(Long cateId, PageRequest pageRequest) {
		org.springframework.data.domain.Page<ArticleEntity> list;
		if (cateId == null) {
			list = articleDao.getArticleListByPage(PageRequestUtils.buildSpringDataPageRequest(pageRequest));
		} else {
			list = articleDao.getArticleListByCateId(cateId, PageRequestUtils.buildSpringDataPageRequest(pageRequest));
		}
		return PageUtils.getPage(list);
	}

	/***
	 * 根据多个分类id获取文章
	 * 
	 * @param cateIds
	 * @param pageRequest
	 * @return
	 * @author chenbinghuang 2017年6月14日
	 */
	public Page<ArticleEntity> getArticleListByCateIds(List<Long> cateIds, PageRequest pageRequest) {
		org.springframework.data.domain.Page<ArticleEntity> list;
		pageRequest.setOrderBy("dateline,orderby");
		pageRequest.setOrderDir("desc,asc");
		if (cateIds.isEmpty() == true) {
			list = articleDao.getArticleListByPage(PageRequestUtils.buildSpringDataPageRequest(pageRequest));
		} else {
			list = articleDao.getArticleListByCateIds(cateIds, PageRequestUtils.buildSpringDataPageRequest(pageRequest));
		}
		return PageUtils.getPage(list);
	}

	/***
	 * 根据 标题 模糊查找文章
	 * 
	 * @param searchValue
	 * @param pageRequest
	 * @return
	 * @author chenbinghuang 2017年6月14日
	 */
	public Page<ArticleEntity> searchArticle(String searchValue, PageRequest pageRequest) {
		org.springframework.data.domain.Page<ArticleEntity> list = articleDao.getArticleListByPage(searchValue, PageRequestUtils.buildSpringDataPageRequest(pageRequest));
		return PageUtils.getPage(list);
	}

	/***
	 * 获取文章内容
	 * 
	 * @param id
	 * @return
	 * @author chenbinghuang 2017年6月14日
	 */
	public ArticleContentEntity getContentById(Long id) {
		if (id != null) {
			return articleContentDao.findOne(id);
		}
		return null;
	}

	/***
	 * 保存文章评论
	 * 
	 * @param comment
	 * @throws BusinessException
	 * @author chenbinghuang 2017年6月14日
	 */
	public void saveArticleComment(ArticleCommentEntity comment) throws BusinessException {
		articleCommentDao.save(comment);
	}

	/***
	 * 根据文章id 获取文章评论
	 * 
	 * @param articleId
	 * @param pageRequest
	 * @return
	 * @author chenbinghuang 2017年6月14日
	 */
	public Page<ArticleCommentEntity> getArticleCommentList(Long articleId, PageRequest pageRequest) {
		org.springframework.data.domain.Page<ArticleCommentEntity> list = articleCommentDao.getCommentList(articleId, PageRequestUtils.buildSpringDataPageRequest(pageRequest));
		return PageUtils.getPage(list);
	}

	/***
	 * 获取前一篇文章
	 * 
	 * @param articleId
	 * @return
	 * @author chenbinghuang 2017年6月14日
	 */
	public ArticleEntity getPrevArticle(Long articleId) {
		return articleDao.getPrevArticle(articleId);
	}

	/***
	 * 获取下一篇文章
	 * 
	 * @param articleId
	 * @return
	 * @author chenbinghuang 2017年6月14日
	 */
	public ArticleEntity getNextArticle(Long articleId) {
		return articleDao.getNextArticle(articleId);
	}

	/***
	 * 增加文章浏览数
	 * 
	 * @param articleId
	 * @throws BusinessException
	 * @author chenbinghuang 2017年6月14日
	 */
	public void addView(Long articleId) throws BusinessException {
		if (articleId == null || articleId == 0) {
			throw new BusinessException("文章id为空");
		}
		ArticleEntity entity = articleDao.findOne(articleId);
		Long views = entity.getViews();
		if (views == null) {
			views = 0L;
		}
		views += 1;
		entity.setViews(views);
		articleDao.save(entity);
	}

	/**
	 * 添加或修改文章
	 * 
	 * @param articleDto
	 * @param user
	 * @throws BusinessException
	 * @author chenbinghuang 2017年6月14日
	 */
	@Transactional
	public void saveOrUpdate(ArticleDto articleDto, UserModel user, long sessionIdHashCode) throws BusinessException {
		if (StringUtils.isEmpty(articleDto.getTitle())) {
			throw new BusinessException("文章标题不能为空");
		}
		if (articleDto.getCateId() == null || articleDto.getCateId() <= 0) {
			throw new BusinessException("文章分类不能为空");
		}
		if (StringUtils.isEmpty(articleDto.getContent())) {
			throw new BusinessException("文章内容不能为空");
		}
		if (articleDto == null || articleDto.getOrderby() < 0) {
			throw new BusinessException("排序不能小于0");
		}

		if (articleDto.getId() == null) {
			ArticleEntity articleEntity = new ArticleEntity();

			articleEntity.setAllowComment(articleDto.getAllowComment());
			articleEntity.setCateId(articleDto.getCateId());
			articleEntity.setClosed(articleDto.getClosed());
			articleEntity.setDescription(articleDto.getDescription());
			articleEntity.setAudit(true);
			articleEntity.setTitle(articleDto.getTitle());
			if (!StringUtils.isEmpty(articleDto.getPhoto())) {
				articleEntity.setPhoto(articleDto.getPhoto());
			}
			articleEntity.setLinkurl(articleDto.getLinkurl());
			articleEntity.setOrderby(articleDto.getOrderby());
			articleEntity = articleDao.save(articleEntity);
			ArticleContentEntity articleContentEntity = new ArticleContentEntity();
			articleContentEntity.setArticleId(articleEntity.getId());
			articleContentEntity.setContent(articleDto.getContent());
			articleContentEntity.setSeoTitle(articleDto.getSeoTitle());
			articleContentEntity.setSeoDescription(articleDto.getSeoDescription());
			articleContentEntity.setSeoKeywords(articleDto.getSeoKeywords());
			articleContentEntity.setCreateBy(user.getLoginname());
			articleContentDao.save(articleContentEntity);

			// 把当前用户未归类的相册图片归类到当前文章下
			PageRequest pageRequest = new PageRequest();
			pageRequest.setPageNo(1);
			pageRequest.setPageSize(1000);

			org.springframework.data.domain.Page<AccessoryEntity> page = accessoryDao.findByModuleIdAndAlbumIdAndDeleteStatus(
					AccessoryEntity.MODULE_ID_ENUM.MODULE_ID_ARTICLE.getValue(), sessionIdHashCode, false, PageRequestUtils.buildSpringDataPageRequest(pageRequest));
			for (AccessoryEntity accessoryEntity : page.getContent()) {
				accessoryEntity.setAlbumId(articleEntity.getId());
				accessoryDao.save(accessoryEntity);
			}
		} else {
			// 更新
			ArticleEntity articleTemp = articleDao.findOne(articleDto.getId());
			// 保存article
			articleTemp.setAllowComment(articleDto.getAllowComment());
			articleTemp.setCateId(articleDto.getCateId());
			articleTemp.setClosed(articleDto.getClosed());
			articleTemp.setDescription(articleDto.getDescription());
			articleTemp.setAudit(true);
			articleTemp.setTitle(articleDto.getTitle());
			if (!StringUtils.isEmpty(articleDto.getPhoto())) {
				articleTemp.setPhoto(articleDto.getPhoto());
			}
			articleTemp.setLinkurl(articleDto.getLinkurl());
			articleTemp.setOrderby(articleDto.getOrderby());
			articleTemp = articleDao.save(articleTemp);
			// 保存articleContent
			ArticleContentEntity articleContentTemp = articleContentDao.getArticleContentByArticleId(articleTemp.getId());
			articleContentTemp.setContent(articleDto.getContent());
			articleContentTemp.setSeoTitle(articleDto.getSeoTitle());
			articleContentTemp.setSeoDescription(articleDto.getSeoDescription());
			articleContentTemp.setSeoKeywords(articleDto.getSeoKeywords());

			articleContentTemp.setLastModifyBy(user.getLoginname());
			articleContentTemp.setLastModifyTime(new Date());

			articleContentDao.save(articleContentTemp);
		}

	}
}
